/*
 *  Copyright (c)  09.04.2007 Philipp Marx.
 *  Permission is granted to copy, distribute and/or modify this document
 *  under the terms of the GNU Free Documentation License, Version 1.2
 *  or any later version published by the Free Software Foundation;
 *  with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts.
 *  A copy of the license is included in the section entitled "GNU
 *  Free Documentation License".
 */

package de.smigfu.irc.handler;

import de.smigfu.irc.core.IRCProtocolConstants;
import de.smigfu.irc.core.IRCServerConnection;

/**
 * A <code>PrivMessageHandler</code> is an implementation of an <code>MessageHandler</code> 
 * specialized to handle a CMD_PRIVMSG. A CMD_PRIVMSG is a "normal" message send by another user. 
 * A special method is provided which to handle a CMD_PRIVMSG.
 *
 * @version $Revision$ $Date$
 *
 * @author Philipp Marx, last modified by $Author$
 */
public abstract class PrivMessageHandler implements MessageHandler {

   /**
    * If a PRIVMSG is detected, the message delegated to {@link #handlePrivMessageReceived(IRCServerConnection, Message, String, String, String)}.
    * 
    * @see de.smigfu.irc.handler.MessageHandler#handleMessageReceived(IRCServerConnection, Message)
    */
   public final void handleMessageReceived(final IRCServerConnection connection,
                                           final Message message) {

      // Is it a CMD_PRIVMSG.
      if (message.getCommand().equals(IRCProtocolConstants.CMD_PRIVMSG)) {
         final String params = message.getParams();

         final String source = message.getNickName();

         final int destStart = 0;
         final int destEnd = params.indexOf(':', destStart);
         final String dest = params.substring(destStart, destEnd);

         final int contentStart = destEnd + 1;
         final int contentEnd = params.length();
         final String content = params.substring(contentStart, contentEnd);

         this.handlePrivMessageReceived(connection, message, content, source, dest);
      }
   }

   /**
    * This method must be implemented. It will be invoked every time a PRIVMSG is received by the
    * <code>IRCServerConnection</code>.
    * 
    * @param connection The <code>IRCServerConnection</code> which received the message (the subject).
    * @param message The received message.
    * @param content The actual content of the received message.
    * @param source The source of the message.
    * @param destination The destination of the message (this user or a channel).
    */
   public abstract void handlePrivMessageReceived(IRCServerConnection connection,
                                                  Message message,
                                                  String content,
                                                  String source,
                                                  String destination);
}
